package com.longmao.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longmao.eduservice.entity.EduSubject;
import com.longmao.eduservice.entity.excel.SubjectData;
import com.longmao.eduservice.entity.subject.OneSubject;
import com.longmao.eduservice.entity.subject.TwoSubject;
import com.longmao.eduservice.listener.SubjectExcelListener;
import com.longmao.eduservice.mapper.EduSubjectMapper;
import com.longmao.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.management.ObjectName;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author longmao
 * @since 2022-01-26
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 课程分类列表（树形）
     * @return
     */
    @Override
    public List<OneSubject> getAllSubject() {

        //获得一级分类数据记录
        QueryWrapper<EduSubject> onewrapper = new QueryWrapper<>();
        onewrapper.eq("parent_id", 0);
        List<EduSubject> oneSubjects = baseMapper.selectList(onewrapper);


        //获得二级分类数据记录
        QueryWrapper<EduSubject> twowrapper = new QueryWrapper<>();
        twowrapper.ne("parent_id", 0);
        List<EduSubject> twoSubjects = baseMapper.selectList(twowrapper);

        List<OneSubject> subjectTreeOne = new ArrayList<>();

        //封装一级分类数据
        //查询出所有一级分类list集合遍历
        for (int i = 0; i < oneSubjects.size(); i++) {
            //得到每一个一级分类对象,获得每一个一级分类对象值
            EduSubject eduSubject = oneSubjects.get(i);

            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject, oneSubject);
            subjectTreeOne.add(oneSubject);

            //封装二级分类数据
            List<TwoSubject> subjectTreeTwo = new ArrayList<>();

            for (int j = 0; j < twoSubjects.size(); j++) {
                eduSubject = twoSubjects.get(j);
                //判断二级分类的id是否和一级分类的id一致
                if(eduSubject.getParentId().equals(oneSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject,twoSubject);
                    subjectTreeTwo.add(twoSubject);
                }
            }
            oneSubject.setChildren(subjectTreeTwo);
        }

        return subjectTreeOne;
    }
}
